<?php

require FCPATH . 'application/controllers/backend/backend.php';

class Entry extends Backend {

    public function __construct() {
	parent::__construct();

	$this->load->model('entry_model', 'model');
    }

    public function index() {
	$data = array(
	    'type' => intval($this->uri->segment(4))
	);

	$this->load_javascripts('backend_entry_index');

	$this->set_javascript_variable('TYPE', $data['type']);

	$this->rend('entry_index_view', $data);
    }

    public function add() {
	$data = array(
	    'type' => intval($this->uri->segment(4))
	);

	$this->load_stylesheets('../js/ueditor/themes/default/ueditor');

	$this->load_javascripts(array(
	    'ueditor/editor_config',
	    'ueditor/editor_all',
	    'uploadify/swfobject',
	    'uploadify/jquery.uploadify',
	    'backend_entry_form'
	));

	$this->rend('entry_add_view', $data);
    }

    public function insert() {
	$data = array(
	    'album' => intval($this->input->post('album')),
	    'type' => intval($this->input->post('type')),
	    'title' => html_entity_decode(trim($this->input->post('title'))),
	    'title_en' => html_entity_decode(trim($this->input->post('title_en'))),
	    'content' => trim($this->input->post('content')),
	    'content_en' => trim($this->input->post('content_en')),
	    'display' => intval($this->input->post('display')),
	    'created_at' => date('Y-m-d', time())
	);

	if (empty($data['title'])) {
	    $this->json_back('XT002', '中文标题不能为空');
	}

	if (empty($data['content'])) {
	    $this->json_back('XT002', '中文内容不能为空');
	}

	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '英文标题不能为空');
	}

	if (empty($data['content_en'])) {
	    $this->json_back('XT002', '英文内容不能为空');
	}

	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '新增成功', array('id' => $id));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }

    public function edit() {
	$id = intval($this->uri->segment(4));

	$this->load_stylesheets('../js/ueditor/themes/default/ueditor');

	$this->load_javascripts(array(
	    'ueditor/editor_config',
	    'ueditor/editor_all_min',
	    'uploadify/swfobject',
	    'uploadify/jquery.uploadify',
	    'backend_entry_form'
	));

	$data = array(
	    'entry' => $this->model->find($id)
	);

	if ($data['entry']->album > 0) {
	    $this->load->model('photo_model');
	    $data['photos'] = $this->photo_model->records(0, 0, array(array('album', $data['entry']->album)));
	}

	$this->rend('entry_edit_view', $data);
    }

    public function update() {
	$id = intval($this->input->post('id'));

	$data = array(
	    'album' => intval($this->input->post('album')),
	    'title' => html_entity_decode(trim($this->input->post('title'))),
	    'title_en' => html_entity_decode(trim($this->input->post('title_en'))),
	    'content' => trim($this->input->post('content')),
	    'content_en' => trim($this->input->post('content_en')),
	    'display' => intval($this->input->post('display'))
	);

	if (empty($data['title'])) {
	    $this->json_back('XT002', '中文标题不能为空');
	}
	if (empty($data['content'])) {
	    $this->json_back('XT002', '中文内容不能为空');
	}
	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '英文标题不能为空');
	}
	if (empty($data['content_en'])) {
	    $this->json_back('XT002', '英文内容不能为空');
	}

	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '修改成功', array('id' => $id));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }

    public function show() {
	$id = intval($this->uri->segment(4));

	$data = array(
	    'type' => intval($this->uri->segment(5)),
	    'entry' => $this->model->find($id)
	);

	if ($data['entry']->album > 0) {
	    $this->load->model('photo_model');
	    $data['photos'] = $this->photo_model->records(0, 0, array(array('album', $data['entry']->album)));
	}

	$this->rend('entry_show_view', $data);
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));

	$page = $page <= 0 ? 1 : $page;
	$page_size = $page_size <= 0 ? 20 : $page_size;

	$offset = $page_size * ($page - 1);

	$type = intval($this->input->get('type'));

	$conditions = array();

	if ($type) {
	    $conditions[] = array('type', $type);
	}

	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records($page_size, $offset, $conditions)
	));
    }

    public function delete() {
	$id = intval($this->uri->segment(4));
	
	$entry = $this->model->find($id);
	
	if($entry && intval($entry->album) > 0){
	    $this->delete_photos($entry->album);
	}
	
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功', array('id' => $id));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }
    
    private function delete_photos($album){
	$this->load->model('photo_model');
	$this->load->model('album_model');
	$conditions = array(array('album',$album));
	$photos = $this->photo_model->records(0,0,$conditions);
	
	$this->album_model->delete($album);
	
	$this->photo_model->delete($conditions);
	
	if(isset($photos[0])){
	    foreach($photos as $photo){
		$path = FCPATH.'/assets/uploads/'.$photo->file_name;
		if(file_exists($path)){
		    @unlink($path);
		}
	    }
	}
    }

}